package com.hdax.dm.service;

import com.alibaba.fastjson.JSON;
import com.hdax.dm.entity.scheduler.SchedulerSeat;
import com.hdax.dm.feign.scheduler.SchedulerSeatFeign;
import com.hdax.dm.vo.GetScheduleVo;
import com.hdax.dm.vo.SeatPriceVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class SchedulerSeatService {

    @Autowired
    private SchedulerSeatFeign schedulerSeatFeign;
    @Autowired
    private SchedulerSeatPriceService schedulerSeatPriceService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 获取座位详细信息
     * @param schedulerId
     * @param cinemaId
     * @return
     */
    public GetScheduleVo seatInfoList(Long schedulerId,Long cinemaId){

        if (cinemaId != null){
            redisTemplate.opsForValue().set("cinemaId", JSON.toJSONString(cinemaId),24L, TimeUnit.HOURS);
        } else {
            cinemaId = redisTemplate.opsForValue().get("cinemaId") == null?0:Long.parseLong(redisTemplate.opsForValue().get("cinemaId"));
        }

        List<SeatPriceVo> seatPrices = schedulerSeatPriceService.getPrice(schedulerId);
        List<SchedulerSeat> seats = schedulerSeatFeign.getScheduleSeatList(schedulerId);
        GetScheduleVo vo = new GetScheduleVo();
        vo.setScheduleId(schedulerId);
        vo.setCinemaId(cinemaId);
        System.out.println(vo.getCinemaId());
        //座位价格
        List<GetScheduleVo.SeatPriceList> sps = new ArrayList<>();
        //座位详情
        List<GetScheduleVo.SeatInfoList> sis = new ArrayList<>();
        //给详情赋值
        seats.forEach(seat->{
            GetScheduleVo.SeatInfoList si = new GetScheduleVo.SeatInfoList();
            BeanUtils.copyProperties(seat,si);

            Long id = si.getId();
            Long cinema = schedulerSeatFeign.getScheduleSeatById(id);
            si.setCinemaId(cinema == null?null:cinema);

            if (seat.getAreaLevel() == 1){
                si.setAreaLevel("A");
            } else if (seat.getAreaLevel() == 2){
                si.setAreaLevel("B");
            } else if (seat.getAreaLevel() == 3){
                si.setAreaLevel("C");
            }
            sis.add(si);
        });
        vo.setSeatInfoList(sis);
        //给价格赋值
        seatPrices.forEach(seatPrice->{
            GetScheduleVo.SeatPriceList sp = new GetScheduleVo.SeatPriceList();
            if (seatPrice.getAreaLevel() == 1){
                sp.setAreaLevelName("A");
            } else if (seatPrice.getAreaLevel() == 2){
                sp.setAreaLevelName("B");
            } else if (seatPrice.getAreaLevel() == 3){
                sp.setAreaLevelName("C");
            }
            BeanUtils.copyProperties(seatPrice,sp);
            sps.add(sp);
        });
        vo.setSeatPriceList(sps);
        return vo;
    }

    /**
     * 查询座位状态并返回给前端显示
     * @param scheduleId
     * @return
     */
    public Map<String,List<String>> getSeatList(Long scheduleId){
        List<SchedulerSeat> list = schedulerSeatFeign.getScheduleSeatList(scheduleId);
        List<String> seats = new ArrayList<>();
        Map<Long, List<SchedulerSeat>> map = list.stream().collect(Collectors.groupingBy(SchedulerSeat::getX));
        map.forEach((key,value)->{
            StringBuffer buffer = new StringBuffer();
            value.forEach(seat->{
                if (seat.getStatus() == 0){
                    buffer.append("_");
                } else if (seat.getStatus() == 1){
                    if (seat.getAreaLevel() == 1){
                        buffer.append("a");
                    } else if (seat.getAreaLevel() == 2){
                        buffer.append("b");
                    } else {
                        buffer.append("c");
                    }
                }
            });
            seats.add(buffer.toString());
        });
        Map<String,List<String>> map1 = new HashMap<>();
        map1.put("seatArray",seats);
        return map1;
    }

}
